﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Model.Extend.Models;
using Estsh.Core.Models;
using Estsh.Core.Services;
using Estsh.Core.Wms.IRepositories;
using Estsh.Core.Wms.IServices;
using System;
using System.Collections.Generic;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Estsh.Core.Wms.Services
{
    /// <summary>
    /// 零星入库
    /// </summary>
    public class ScatteredInStockService : BaseService<BaseEntity>, IScatteredInStockService
    {
        private readonly IScatteredInStockRepository repository;
        public ScatteredInStockService(IScatteredInStockRepository _repository) : base(_repository)
        {
            repository = _repository;
        }
        public SetObjectDetail GetScatteredInStockOrderList(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            List<WmsInstock> wmsMoves = repository.GetScatteredInStockOrderList(orderNo);
            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail GetScatteredInStockOrderListByOrderNo(string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();

            List<WmsInstock_Extend> wmsMoves = repository.GetScatteredInStockOrderListByOrderNo(orderNo);
            if (wmsMoves.Count > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "成功";
                rfInfo.wmsObjectDetail = wmsMoves.ConvertAll(s => (object)s);
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }

        public SetObjectDetail GetLocateByName(string locateName, string orderNo)
        {
            SetObjectDetail rfInfo = new SetObjectDetail();
            SysLocate sysLocates = repository.GetLocateByLocateName(locateName);

            SysStock sysStocksLocateCount = repository.GetStackQtySumByLocateName(locateName);//库位在库数

            if (sysLocates == null)
            {
                //提示  [A1-2-2]:该库位不存在，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不存在，请检查!";
                return rfInfo;
            }
            else if (sysLocates.Enabled != "Y")
            {
                //提示  [A1-2-2]:该库位不可用，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位不可用，请检查!";
                return rfInfo;
            }
            else if (sysStocksLocateCount.Qty >= sysLocates.LocateCapacity)
            {
                //提示  [A1-2-2]:该库位容量已满，请检查!
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + locateName + "]:该库位容量已满，请检查!";
                return rfInfo;
            }

            //List<WmsInstockDetail> instockDetailsErp = repository.GetInstockByOrderNoDestErp(orderNo, sysLocates.ErpWarehouse);
            //if (instockDetailsErp.Count == 0)
            //{
            //    rfInfo.type = "FAIL";
            //    rfInfo.message = "[" + locateName + "]:该库位对应库存地不在单据内!";
            //    return rfInfo;
            //}

            rfInfo.type = "PASS";
            rfInfo.message = "[" + locateName + "]:扫描成功,请扫描箱条码";
            return rfInfo;
        }

        public SetObjectDetail CheckStockByCartonAlocate(string cartonNo, string locateName, string orderNo, string loginId)
        {
            List<SysStock> sysStockList = null;
            string strCartonNo = "";
            int refNum = 0;
            SetObjectDetail rfInfo = new SetObjectDetail();
            StringBuilder stringBuilder = new StringBuilder(1024);
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();

            SysStock sysStocks = repository.GetStockByCartonNo(cartonNo);
            if (sysStocks.CartonType == 1)
            {
                //获取周转箱中的物料
                List<WmsRackPackage_Extend> rackPackageInfo = repository.GetCartonInfo(cartonNo);
                List<WmsInstockDetail> instockDetails = repository.GetInstockOrderNo(orderNo);

                //判断周转箱内物料是否和单据物料一致
                for (int i = 0; i < rackPackageInfo.Count; i++)
                {
                    List<WmsInstockDetail> outstockDetails1 = instockDetails.Where(x => x.PartNo == rackPackageInfo[i].PartNo).ToList();
                    if (outstockDetails1.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "周转箱内物料与单据物料不一致！";
                        return rfInfo;
                    }
                    else
                    {
                        List<WmsInstockDetail> outstockDetails2 = instockDetails.Where(x => x.PartNo == rackPackageInfo[i].PartNo && x.Qty >= Convert.ToInt32(rackPackageInfo[i].RackNum)).ToList();
                        if (outstockDetails2.Count == 0)
                        {
                            rfInfo.type = "FAIL";
                            rfInfo.message = "[" + cartonNo + "]:周转箱下条码数量大于单据需求数量!";
                            return rfInfo;
                        }
                    }
                    strCartonNo += rackPackageInfo[i].CortonNo + "','";
                }
                if (strCartonNo.EndsWith("'"))
                    strCartonNo = strCartonNo.Substring(0, strCartonNo.Length - 3);
                sysStockList = repository.GetStockByCartonNoList(strCartonNo);
            }
            else
            {
                sysStockList = repository.GetStockByCartonNoList(cartonNo);

                //扫描的箱条码，与周转箱进行解绑
                List<WmsRackPackage_Extend> rackPackageCartNos = repository.GetRackPackageCartons(cartonNo);
                if (rackPackageCartNos.Count != 0)
                {
                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("UPDATE dbo.wms_rack_package SET enabled='N' WHERE ruid='" + rackPackageCartNos.FirstOrDefault().Ruid + "';");
                    sqlStrings.Add(stringBuilder.ToString());
                }
            }

       
            if (sysStockList.Count > 0)
            {
                for (int j = 0; j < sysStockList.Count; j++)
                {
                    if (sysStockList[j].Enabled != "Y")
                    {
                        //提示  [M0090105]:箱条码已冻结，无法操作!
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + sysStockList[j].CartonNo + "]:条码已冻结，无法操作!";
                        return rfInfo;
                    }
                    else if (sysStockList[j].Status != (int)WmsEnumUtil.StockStatus.OUTSTOCKED)//已出库
                    {
                        //提示  [M0090105]:该条码是**状态，无法操作!
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + sysStockList[j].CartonNo + "]:该条码是[" + sysStockList[j].StockStatus + "]状态，请检查!";
                        return rfInfo;
                    }
                    List<WmsInstockSn> moveSnCreates = repository.GetInStockSnByOrderNo(orderNo);
                    if (moveSnCreates.Count > 0)
                    {
                        List<WmsInstockSn> WmsInstockSns = repository.GetInStockSnByOrderNoCartonNo(orderNo, sysStockList[j].CartonNo);
                        if (WmsInstockSns.Count == 0)
                        {
                            rfInfo.type = "FAIL";
                            rfInfo.message = "[" + sysStockList[j].CartonNo + "]:单据中不包含此零件[" + sysStockList[j].PartNo + "]包装条码，不允许此操作！";
                            return rfInfo;
                        }
                    }
                    SysLocate sysLocates = repository.GetLocateInfoByLocateName(locateName);

                    string locatePartNo1 = sysLocates.PartNo;
                    string stockPartNo1 = sysStockList[j].PartNo;
                    if (sysLocates.LocateType == (int)WmsEnumUtil.LocateType.BASICS_LOCATE)
                    {
                        if (locatePartNo1 != stockPartNo1)
                        {
                            //提示  此箱码所对应零件与库位零件不匹配!
                            rfInfo.type = "FAIL";
                            rfInfo.message = "此箱码所对应零件与库位零件不匹配!";
                            return rfInfo;
                        }
                    }
                    else if (sysLocates.LocateType == (int)WmsEnumUtil.LocateType.MANEUVER_LOCATE)//机动库位
                    {
                        List<SysStock> sysStocks3 = repository.GetInStockByLocateName(sysLocates.LocateName);
                        for (int i = 0; i < sysStocks3.Count; i++)
                        {
                            if (sysStocks3[i].PartNo != "" && stockPartNo1 != sysStocks3[i].PartNo)
                            {
                                //提示  此箱码所对应零件与库位零件不匹配!
                                rfInfo.type = "FAIL";
                                rfInfo.message = "[" + stockPartNo1 + "]该零件号无法入到机动库位，请检查!";
                                return rfInfo;
                            }
                        }
                    }
                    List<WmsInstock> WmsInstocks = repository.GetInStockByOrderNo(orderNo);
                    if (WmsInstocks.Count == 0)
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + orderNo + "]:该单据是未创建或已关闭状态，请检查!";
                        return rfInfo;
                    }

                    List<WmsInstockDetail> WmsInstockDetails = repository.GetInStockDetailByOrderNoPartNo(orderNo, sysStockList[j].PartNo);
                    if (WmsInstockDetails.Count == 0)
                    {
                        //提示  [M0090105]:该条码是零星入库状态，无法操作!
                        rfInfo.type = "FAIL";
                        rfInfo.message = "[" + sysStockList[j].CartonNo + "]:该条码零件号[" + sysStockList[j].PartNo + "]不在单据内!";
                        return rfInfo;
                    }

                    for (int i = 0; i < WmsInstockDetails.Count; i++)
                    {
                        int remainNum = Convert.ToInt32(WmsInstockDetails[i].Qty - WmsInstockDetails[i].RecQty);//余数
                        if (sysStockList[j].Qty == remainNum)
                        {
                            if (WmsInstocks[0].OrderStatus != (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING)//
                            {
                                stringBuilder = new StringBuilder(1024);
                                stringBuilder.Append("UPDATE wms_instock SET order_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                                sqlStrings.Add(stringBuilder.ToString());
                            }
                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE wms_instock_detail SET rec_qty+=" + sysStockList[j].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + " ,update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + WmsInstockDetails[i].Ruid + "';");
                            sqlStrings.Add(stringBuilder.ToString());

                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE sys_stock SET status=" + (int)WmsEnumUtil.StockStatus.INSTOCKED + ",locate_id=" + sysLocates.LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates.ZoneId + ",zone_name='" + sysLocates.ZoneName + "',warehouse_id=" + sysLocates.WarehouseId + ",warehouse_name='" + sysLocates.WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStockList[j].CartonNo + "';");
                            sqlStrings.Add(stringBuilder.ToString());

                            stringBuilder = new StringBuilder(1024);
                            //更新单据条码表
                            stringBuilder.Append("UPDATE dbo.wms_instock_sn ");
                            stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                            stringBuilder.Append("    update_userid = " + loginId + ", ");
                            stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                            stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                            stringBuilder.Append("      AND carton_no = '" + sysStockList[j].CartonNo + "'; ");
                            sqlStrings.Add(stringBuilder.ToString());
                            break;
                        }
                        else if (sysStockList[j].Qty > remainNum)
                        {
                            rfInfo.type = "FAIL";
                            rfInfo.message = "[" + sysStockList[j].CartonNo + "]:该条码库存大于单据需求数量!";
                            return rfInfo;
                        }
                        else if (sysStockList[j].Qty < remainNum)
                        {
                            if (WmsInstocks[0].OrderStatus != (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING)//
                            {
                                //处理中
                                stringBuilder = new StringBuilder(1024);
                                stringBuilder.Append("UPDATE wms_instock SET order_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no='" + orderNo + "';");
                                sqlStrings.Add(stringBuilder.ToString());
                            }

                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE wms_instock_detail SET rec_qty+=" + sysStockList[j].Qty + ",item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.BATCHING + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  ruid='" + WmsInstockDetails[i].Ruid + "';");
                            sqlStrings.Add(stringBuilder.ToString());

                            stringBuilder = new StringBuilder(1024);
                            stringBuilder.Append("UPDATE sys_stock SET status=" + (int)WmsEnumUtil.StockStatus.INSTOCKED + ",locate_id=" + sysLocates.LocateId + ",locate_name='" + locateName + "',zone_id=" + sysLocates.ZoneId + ",zone_name='" + sysLocates.ZoneName + "',warehouse_id=" + sysLocates.WarehouseId + ",warehouse_name='" + sysLocates.WarehouseName + "',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  carton_no='" + sysStockList[j].CartonNo + "';");
                            sqlStrings.Add(stringBuilder.ToString());

                            stringBuilder = new StringBuilder(1024);
                            //更新单据条码表
                            stringBuilder.Append("UPDATE dbo.wms_instock_sn ");
                            stringBuilder.Append("SET status = " + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ", ");
                            stringBuilder.Append("    update_userid = " + loginId + ", ");
                            stringBuilder.Append("    update_time = CONVERT(VARCHAR(20),GETDATE(),21) ");
                            stringBuilder.Append("WHERE order_no = '" + orderNo + "' ");
                            stringBuilder.Append("      AND carton_no = '" + sysStockList[j].CartonNo + "'; ");
                            sqlStrings.Add(stringBuilder.ToString());
                            break;
                        }
                    }

                    stringBuilder = new StringBuilder(1024);
                    stringBuilder.Append("INSERT INTO dbo.sys_stock_trans ");
                    stringBuilder.Append("( ");
                    stringBuilder.Append("    trans_code, ");
                    stringBuilder.Append("    carton_no, ");
                    stringBuilder.Append("    part_id, ");
                    stringBuilder.Append("    part_no, ");
                    stringBuilder.Append("    part_spec, ");
                    stringBuilder.Append("    src_locate_id, ");
                    stringBuilder.Append("    src_locate_name, ");
                    stringBuilder.Append("    dest_locate_id, ");
                    stringBuilder.Append("    dest_locate_name, ");
                    stringBuilder.Append("    old_qty, ");
                    stringBuilder.Append("    new_qty, ");
                    stringBuilder.Append("    trans_qty, ");
                    stringBuilder.Append("    old_status, ");
                    stringBuilder.Append("    new_status, ");
                    stringBuilder.Append("    unit, ");
                    stringBuilder.Append("    factory_id, ");
                    stringBuilder.Append("    factory_code, ");
                    stringBuilder.Append("    src_erp_warehouse, ");
                    stringBuilder.Append("    dest_erp_warehouse, ");
                    stringBuilder.Append("    src_warehouse_id, ");
                    stringBuilder.Append("    src_warehouse_name, ");
                    stringBuilder.Append("    dest_warehouse_id, ");
                    stringBuilder.Append("    dest_warehouse_name, ");
                    stringBuilder.Append("    src_zone_id, ");
                    stringBuilder.Append("    src_zone_name, ");
                    stringBuilder.Append("    dest_zone_id, ");
                    stringBuilder.Append("    dest_zone_name, ");
                    stringBuilder.Append("    enabled, ");
                    stringBuilder.Append("    create_userid, ");
                    stringBuilder.Append("    create_time, ");
                    stringBuilder.Append("    update_userid, ");
                    stringBuilder.Append("    update_time, ");
                    stringBuilder.Append("    guid ");
                    stringBuilder.Append(") ");
                    stringBuilder.Append("VALUES ");
                    stringBuilder.Append(" ('" + (int)WmsEnumUtil.TransType.SCATTERED_INSTOCK + "', ");//零星入库
                    stringBuilder.Append("    '" + sysStockList[j].CartonNo + "', ");
                    stringBuilder.Append("    " + sysStockList[j].PartId + ", ");
                    stringBuilder.Append("    '" + sysStockList[j].PartNo + "',  ");
                    stringBuilder.Append("    N'" + sysStockList[j].PartSpec + "', ");
                    stringBuilder.Append("    " + sysStockList[j].LocateId + ",  ");//源库位
                    stringBuilder.Append("    '" + sysStockList[j].LocateName + "',  ");
                    stringBuilder.Append("    " + sysLocates.LocateId + ", ");//目标库位
                    stringBuilder.Append("    '" + sysLocates.LocateName + "',  ");
                    stringBuilder.Append("    " + sysStockList[j].Qty + ",");
                    stringBuilder.Append("    " + sysStockList[j].Qty + ",");
                    stringBuilder.Append("    " + sysStockList[j].Qty + ",");
                    stringBuilder.Append("    '" + sysStockList[j].Status + "', ");
                    stringBuilder.Append("    '" + (int)WmsEnumUtil.StockStatus.INSTOCKED + "', ");
                    stringBuilder.Append("    '" + sysStockList[j].Unit + "', ");
                    stringBuilder.Append("    " + sysStockList[j].FactoryId + ", ");
                    stringBuilder.Append("    '" + sysStockList[j].FactoryCode + "',");
                    stringBuilder.Append("    '" + sysStockList[j].ErpWarehouse + "',");
                    stringBuilder.Append("    '" + sysStockList[j].ErpWarehouse + "', ");
                    stringBuilder.Append("    " + sysStockList[j].WarehouseId + ", ");
                    stringBuilder.Append("    '" + sysStockList[j].WarehouseName + "', ");
                    stringBuilder.Append("    " + sysStockList[j].WarehouseId + ", ");
                    stringBuilder.Append("    '" + sysStockList[j].WarehouseName + "', ");
                    stringBuilder.Append("    " + sysStockList[j].ZoneId + ",  ");
                    stringBuilder.Append("    '" + sysStockList[j].ZoneName + "',  ");
                    stringBuilder.Append("    " + sysStockList[j].ZoneId + ", ");
                    stringBuilder.Append("    '" + sysStockList[j].ZoneName + "',  ");
                    stringBuilder.Append("    'Y', ");
                    stringBuilder.Append("    " + loginId + ", ");
                    stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                    stringBuilder.Append("    " + loginId + ", ");
                    stringBuilder.Append("    CONVERT(VARCHAR(20),GETDATE(),21), ");
                    stringBuilder.Append("    NEWID()  ");
                    stringBuilder.Append("    ) ; ");
                    sqlStrings.Add(stringBuilder.ToString());
                }

                refNum = repository.SQLTransaction(sqlStrings, parameterList);
            }
            else 
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "[" + cartonNo + "]:该条码不存在，请检查!";
                return rfInfo;
            }

            if (refNum > 0)
            {
                List<WmsInstock> wmsMoves = repository.GetInStockInfoByOrderNo(orderNo);

                if (wmsMoves.Count > 0)
                {
                    List<WmsInstockDetail> wmsMoves1 = repository.GetInStockDetailQtySumByOrderNo(orderNo);
                    for (int i = 0; i < wmsMoves1.Count; i++)
                    {
                        if (wmsMoves1[i].Qty != wmsMoves1[i].RecQty)
                        {
                            rfInfo.type = "PASS";
                            rfInfo.message = "操作成功!请继续扫描箱条码";
                            rfInfo.wmsObjectDetail = wmsMoves1.ConvertAll(s => (object)s);
                            return rfInfo;
                        }
                    }

                    int updateStatus = 1;
                    sqlStrings = new List<string>();
                    parameterList = new List<DynamicParameters>();
                    StringBuilder SqlStringBuilder = new StringBuilder(1024);
                    DynamicParameters parameters = new DynamicParameters();

                    List<WmsInstockDetail> wmsInstockDetails = repository.GetFinishInStockDetailByOrderNo(orderNo);
                    if (wmsInstockDetails.Count > 0)
                    {
                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,MATNR,ERFMG ");
                        SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME,SID,RECTIM,SYNFLG ");
                        SqlStringBuilder.Append("           ,GUID) ");
                        SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@MATNR,@ERFMG,@ERFME ");
                        SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@SID,@RECTIM,@SYNFLG ");
                        SqlStringBuilder.Append("           ,@GUID) ");


                        for (int i = 0; i < wmsInstockDetails.Count; i++)
                        {
                            SysPart sysPart = repository.GetPartNOInfoByPartNo(wmsInstockDetails[i].PartNo);
                            sqlStrings.Add(SqlStringBuilder.ToString());
                            parameters = new DynamicParameters();
                            parameters.Add("@ZINSTNO", wmsInstockDetails[i].OrderNo);//盘点凭证号
                            parameters.Add("@ZEILE", (i + 1) * 10);//行号
                            parameters.Add("@WERKS", wmsInstockDetails[i].FactoryCode);//工厂
                            parameters.Add("@LGORT", "");//发出库存地点
                            parameters.Add("@UMLGO", wmsInstockDetails[i].DestErpWarehouse);//接收库存地点
                            parameters.Add("@MATNR", wmsInstockDetails[i].PartNo);//物料编码
                            parameters.Add("@ERFMG", wmsInstockDetails[i].RecQty);//转储数量
                            if (sysPart == null)
                            {
                                parameters.Add("@ERFME", "");//计量单位
                            }
                            else
                            {
                                parameters.Add("@ERFME", sysPart.Unit);//计量单位
                            }
                            parameters.Add("@ACTION", "M07"); //M07:零星入库
                            parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                            parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
                            parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                            parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                            parameters.Add("@SYNFLG", "N");
                            parameters.Add("@GUID", Guid.NewGuid().ToString());
                            parameterList.Add(parameters);
                        }

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_instock SET order_status='40',update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);
                        parameterList.Add(parameters);

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_instock_detail SET item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);
                        parameterList.Add(parameters);

                        SqlStringBuilder = new StringBuilder(1024);
                        SqlStringBuilder.Append("UPDATE wms_instock_sn SET status=" + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                        sqlStrings.Add(SqlStringBuilder.ToString());
                        parameters = new DynamicParameters();
                        parameters.Add("@orderNo", orderNo);
                        parameterList.Add(parameters);

                        updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
                    }
                    if (updateStatus > 0)
                    {
                        rfInfo.type = "PASS";
                        rfInfo.message = "单据操作完成!";
                    }
                    else
                    {
                        rfInfo.type = "FAIL";
                        rfInfo.message = "单据操作操作失败!请检查";
                    }
                }
                else
                {
                    rfInfo.type = "FAIL";
                    rfInfo.message = "未查询到数据，请检查！";
                }
            }
            else
            {
                //提示  操作失败!
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
        public SetObjectDetail SetScatteredInStockOrderNoSubmit(string orderNo, string loginId)
        {
            List<string> sqlStrings = new List<string>();
            List<DynamicParameters> parameterList = new List<DynamicParameters>();
            SetObjectDetail rfInfo = new SetObjectDetail();//返回消息
            StringBuilder SqlStringBuilder = new StringBuilder(1024);
            DynamicParameters parameters = new DynamicParameters();
            int updateStatus = 1;
            List<WmsInstockDetail> wmsInstockDetails = repository.GetFinishInStockDetailByOrderNo(orderNo);
            if (wmsInstockDetails.Count > 0)
            {
                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO dbo.WMS_MOVR(ZINSTNO,ZEILE,WERKS,LGORT,UMLGO,MATNR,ERFMG ");
                SqlStringBuilder.Append("           ,ERFME,ACTION,ZPOST,ZTIME,SID,RECTIM,SYNFLG ");
                SqlStringBuilder.Append("           ,GUID) ");
                SqlStringBuilder.Append("     VALUES(@ZINSTNO,@ZEILE,@WERKS,@LGORT,@UMLGO,@MATNR,@ERFMG,@ERFME ");
                SqlStringBuilder.Append("           ,@ACTION,@ZPOST,@ZTIME,@SID,@RECTIM,@SYNFLG ");
                SqlStringBuilder.Append("           ,@GUID) ");

                for (int i = 0; i < wmsInstockDetails.Count; i++)
                {
                    SysPart sysPart = repository.GetPartNOInfoByPartNo(wmsInstockDetails[i].PartNo);
                    sqlStrings.Add(SqlStringBuilder.ToString());
                    parameters = new DynamicParameters();
                    parameters.Add("@ZINSTNO", wmsInstockDetails[i].OrderNo);//盘点凭证号
                    parameters.Add("@ZEILE", (i + 1) * 10);//行号
                    parameters.Add("@WERKS", wmsInstockDetails[i].FactoryCode);//工厂
                    parameters.Add("@LGORT", "");//发出库存地点
                    parameters.Add("@UMLGO", wmsInstockDetails[i].DestErpWarehouse);//接收库存地点
                    parameters.Add("@MATNR", wmsInstockDetails[i].PartNo);//物料编码
                    parameters.Add("@ERFMG", wmsInstockDetails[i].RecQty);//转储数量
                    if (sysPart == null)
                    {
                        parameters.Add("@ERFME", "");//计量单位
                    }
                    else
                    {
                        parameters.Add("@ERFME", sysPart.Unit);//计量单位
                    }
                    parameters.Add("@ACTION", "M07"); //M07:零星入库
                    parameters.Add("@ZPOST", DateTime.Now.ToString("yyyyMMdd"));//交易日期
                    parameters.Add("@ZTIME", DateTime.Now.ToString("HHmmss"));//交易时间
                    parameters.Add("@SID", Guid.NewGuid().ToString().Replace("-", ""));
                    parameters.Add("@RECTIM", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"));
                    parameters.Add("@SYNFLG", "N");
                    parameters.Add("@GUID", Guid.NewGuid().ToString());
                    parameterList.Add(parameters);
                }

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_instock SET order_status=" + (int)WmsEnumUtil.MoveOrderStatus.COMPLETED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_instock_detail SET item_status=" + (int)WmsEnumUtil.MoveOrderDetailStatus.COMPLETED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("UPDATE wms_instock_sn SET status=" + (int)WmsEnumUtil.MoveOrderSnStatus.PROCESSED + ",update_userid=" + loginId + ",update_time=CONVERT(VARCHAR(20),GETDATE(),21) WHERE  order_no=@orderNo;");
                sqlStrings.Add(SqlStringBuilder.ToString());
                parameters = new DynamicParameters();
                parameters.Add("@orderNo", orderNo);
                parameterList.Add(parameters);

                updateStatus = repository.SQLTransaction(sqlStrings, parameterList);
            }

            if (updateStatus > 0)
            {
                rfInfo.type = "PASS";
                rfInfo.message = "[" + orderNo + "]单据已提交，请重新选择单据";
            }
            else
            {
                rfInfo.type = "FAIL";
                rfInfo.message = "未查询到数据，请检查！";
            }
            return rfInfo;
        }
    }
}
